<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="layui/common/commonHead :: commonHead(~{::title},~{::link},~{::style})">
    <title>分配角色</title>
    <style type="text/css">
        .layui-form-item .layui-input-inline {
            float: left;
            width: 280px;
            margin-right: 10px;
        }
    </style>
    <link rel="stylesheet" th:href="@{/layuiadmin/modules/formSelects/formSelects-v4.css}" href="../../../../static/layuiadmin/modules/formSelects/formSelects-v4.css" media="all">
</head>
<body>

<div class="layui-form"  lay-filter="userEdit" id="userEdit" style="padding: 20px 0 0 0;">
    <input hidden type="text" name="userId" th:value="${user.id}">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
            <input type="text" name="userAccount" disabled th:value="${user.userAccount}" lay-verify="required" placeholder="请输入用户名(必填)" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-inline">
            <select name="roleIds" style="width: 240px;" class="layui-col-md6" xm-select="roleNameSelect"></select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <input class="layui-btn layui-btn-normal" type="button" lay-submit lay-filter="add" id="id-sys-user-add-submit" value="保存">
            <input class="layui-btn layui-btn-primary" onclick="layerClose()" type="button"  lay-filter="close" id="id-sys-user-add-" value="取消">
        </div>
    </div>
</div>

<div th:replace="layui/common/commonJs :: commonJs(~{})"></div>
<script>
    layui.use(['index', 'form', 'upload','formSelects'], function(){
        var _$ = layui.$
            ,form = layui.form
            ,upload = layui.upload
            ,formSelects = layui.formSelects;

        upload.render({
            elem: '#layuiadmin-upload-useradmin'
            ,url: layui.setter.base + 'json/upload/demo.js'
            ,accept: 'images'
            ,method: 'get'
            ,acceptMime: 'image/*'
            ,done: function(res){
                _$(this.item).prev("div").children("input").val(res.data.src)
            }
        });
        //监听锁定操作
        form.on('submit(add)', function(data){
            var field = data.field;
            //提交 Ajax 成功后，静态更新表格中的数据
            _$.post('/sso/admin/user/grantRoles', field, function (data, status) {
                if (data.status == 200){
                    parent.layer.msg("分配角色成功");
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
/*                    var field = parent.$("#user-list-search-form").serializeObject();
                    console.log(field);
                    parent.layui.table.reload("user_list_table",{
                        where:field
                    });*/
                } else {
                    parent.layer.msg("分配失败请重试!");
                }
            })
        });

        formSelects.render("roleNameSelect");
        var url = '/sso/admin/role/allRoleByUserId?userId='+[[${user.id}]];
        formSelects.data('roleNameSelect', 'server', {
            url: url,
            delay: 500
        });

    });
    function layerClose() {
        layui.use(['layer' ], function() {
            var layer = layui.layer;
            var index=parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
            parent.layer.close(index);//关闭弹出层
        })
    }
</script>
</body>
</html>